package com.hp.gxw.service.impl;

import com.hp.gxw.entity.CostCountCompleted;
import com.hp.gxw.entity.CostCountProduction;
import com.hp.gxw.entity.CostProductionIndex;
import com.hp.gxw.mapper.CostCountTempMapper;
import com.hp.gxw.mapper.CountMapper;
import com.hp.gxw.service.ICostCountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huangping
 * @since 2024-02-21
 */
@Service
public class CostCountServiceImpl implements ICostCountService {

    @Resource
    private CostCountTempMapper costCountTempMapper;

    @Resource
    private CountMapper countMapper;

    private static final int commitNum = 2000;

    @Override
    public void generateCountData(String month) throws Exception{
        costCountTempMapper.generateCountTempData(month);
    }

    @Override
    public void completeSave(List<CostCountCompleted> completedList, List<CostCountProduction> productionList) {
        if(completedList.size() > 0){
            for(int j=0;j<(completedList.size()%commitNum==0?completedList.size()/commitNum:completedList.size()/commitNum+1);j++) {
                countMapper.completedSave(completedList.subList(j*commitNum, (j*commitNum+commitNum)>completedList.size()?completedList.size():(j*commitNum+commitNum)));
            }
        }
        if(productionList.size() > 0){
            for(int j=0;j<(productionList.size()%commitNum==0?productionList.size()/commitNum:productionList.size()/commitNum+1);j++) {
                countMapper.productionSave(productionList.subList(j*commitNum, (j*commitNum+commitNum)>productionList.size()?productionList.size():(j*commitNum+commitNum)));
            }
        }
    }

    @Override
    public void arrangeTempData(String month) {
        countMapper.arrangeTempData(month);
    }

    @Override
    public List<CostProductionIndex> queryBalanceOrder(String month) {
        return countMapper.queryBalanceOrder(month.replace("-",""));
    }

    @Override
    public void balance(String month, String sql) {
        countMapper.balance(month, sql);
    }

    @Override
    public void buildIndex(String month) {
        countMapper.buildIndex(month);
    }
}
